home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4535 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  58 lines

  1. Newsgroups: comp.lang.c
  2. Path: usinternet.com!not-for-mail
  3. From: Scott Jibben <sjibben@usinternet.com>
  4. Subject: Re: a question on "return"
  5. Message-ID: <3116320B.41A2@usinternet.com>
  6. Date: Mon, 05 Feb 1996 10:36:27 -0600
  7. Organization: Jibben Software
  8. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  9. MIME-Version: 1.0
  10. References: <4f2ipq$kaf@srvr1.engin.umich.edu>
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13.  
  14. Chih-Jen Lin wrote:
  15. > In C, usually we treat 1 as true and 0 as false.
  16. > However, we usually use return(0) after normal end of an int
  17. > function and return(1) when some errors happen.
  18. > Can some one tell me why we return a false value after normal
  19. > end of a function ?
  20. > Thanks in advance.
  21. > Chih-Jen Lin
  22.  
  23. Although I've never heard of a real reason.  Here's why I write 
  24. C software using 0 (false) for success.  Because, you generally 
  25. can only succeed in one way.  There may be several ways a 
  26. function may fail.  Since there is only one value for false 
  27. (zero) and many values that are true (non-zero).  It is easier 
  28. to let the calling software know what failed by returning a 
  29. non-zero value.
  30.  
  31. If I had written the functions so that a true value meant 
  32. success, then I'd have to find another way to let the calling 
  33. function know how the function failed (i.e. a parameter that is 
  34. a pointer to an int, global variable (yech), etc.).
  35.  
  36. Also, although a value of one is considered true.  I use -1 (~0) 
  37. to represent true.
  38.  
  39. May I suggest a book?  Get a copy of "Writing Solid Code" 
  40. published by Microsoft Press.  It gives many helpful suggestions 
  41. to writing more stable/error detecting code.
  42.  
  43. -- 
  44. Scott Jibben, Jibben Software
  45. Galactic Overlord & Mines of Gorr BBS Door Games
  46. -----------------------------
  47. [EMAIL] sjibben@usinternet.com
  48. [WWW] http://www.usinternet.com/jsw
  49. [FTP] ftp.europa.com /outgoing/DOORS/jibben
  50. [FTP] ftp.cts.com /pub/dferber/jibben
  51. [FIDO] 1:282/115             [BBS] 612-379-8272 (10 USR 28.8 
  52. Couriers)
  53. [VOICE] 612-757-5626         [FAX] 612-757-8687
  54.  
  55.